package com.ruoyi.performance.service.impl;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.performance.domain.TCollectScore;
import com.ruoyi.performance.domain.TPerformanceAssess;
import com.ruoyi.performance.domain.TPerformanceTemplate;
import com.ruoyi.performance.service.ITCollectScoreService;
import com.ruoyi.performance.service.ITPerformanceAssessService;
import com.ruoyi.performance.service.ITPerformanceTemplateService;
import com.ruoyi.system.api.model.LoginUser;
import com.ruoyi.teacher.api.TeacherService;
import com.ruoyi.teacher.domain.TTeacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.performance.mapper.TTeacherAssessMapper;
import com.ruoyi.performance.domain.TTeacherAssess;
import com.ruoyi.performance.service.ITTeacherAssessService;
import com.ruoyi.utils.SendRobotMessageUtil;

/**
 * 考核自评Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-24
 */
@Service
public class TTeacherAssessServiceImpl implements ITTeacherAssessService 
{
    @Autowired
    private TTeacherAssessMapper tTeacherAssessMapper;

    @Autowired
    private ITCollectScoreService collectScoreService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private ITPerformanceTemplateService performanceTemplateService;

    @Autowired
    private ITPerformanceAssessService performanceAssessService;

    /**
     * 查询考核自评
     * 
     * @param id 考核自评主键
     * @return 考核自评
     */
    @Override
    public TTeacherAssess selectTTeacherAssessById(Long id)
    {
        return tTeacherAssessMapper.selectTTeacherAssessById(id);
    }

    /**
     * 查询考核自评列表
     * 
     * @param tTeacherAssess 考核自评
     * @return 考核自评
     */
    @Override
    public List<TTeacherAssess> selectTTeacherAssessList(TTeacherAssess tTeacherAssess)
    {
        String username = SecurityUtils.getUsername();
        if (username.equals("admin")){
            username = null;
        }
        tTeacherAssess.setTeacherName(username);
        return tTeacherAssessMapper.selectTTeacherAssessList(tTeacherAssess);
    }

    /**
     * 新增考核自评
     * 
     * @param tTeacherAssess 考核自评
     * @return 结果
     */
    @Override
    public int insertTTeacherAssess(TTeacherAssess tTeacherAssess)
    {
        return tTeacherAssessMapper.insertTTeacherAssess(tTeacherAssess);
    }

    /**
     * 修改考核自评
     * 
     * @param tTeacherAssess 考核自评
     * @return 结果
     */
    @Override
    public int updateTTeacherAssess(TTeacherAssess tTeacherAssess)
    {
        return tTeacherAssessMapper.updateTTeacherAssess(tTeacherAssess);
    }

    /**
     * 批量删除考核自评
     * 
     * @param ids 需要删除的考核自评主键
     * @return 结果
     */
    @Override
    public int deleteTTeacherAssessByIds(Long[] ids)
    {
        return tTeacherAssessMapper.deleteTTeacherAssessByIds(ids);
    }

    /**
     * 删除考核自评信息
     * 
     * @param id 考核自评主键
     * @return 结果
     */
    @Override
    public int deleteTTeacherAssessById(Long id)
    {
        return tTeacherAssessMapper.deleteTTeacherAssessById(id);
    }

    /***
     * 修改状态
     * @param id
     * @return
     */
    @Override
    public String changeState(Long id) throws Exception {
        TTeacherAssess tTeacherAssess = tTeacherAssessMapper.selectTTeacherAssessById(id);

        if(tTeacherAssess.getAuditState() ==2){
            throw new GlobalException("你完成自评,不能在自评了！！");
        }
        tTeacherAssess.setAuditState(TTeacherAssess.FINISHED_ASSESS);
        tTeacherAssessMapper.updateTTeacherAssess(tTeacherAssess);
        AjaxResult ajaxResult = teacherService.findTeacherByName(tTeacherAssess.getTeacherName());
        if (!ajaxResult.isSuccess()){
            return "失败";
        }
        Object data = ajaxResult.get("data");
        TTeacher tTeacher = JSONObject.parseObject(JSONObject.toJSONString(data), TTeacher.class);
        String phone = tTeacher.getPhone();
        SendRobotMessageUtil.ShareTip(phone);
        return "自评成功";
    }

    /**
     * 完成审核
     * @param id
     * @return
     */
    @Override
    public String auditPass(Long id) {
        TTeacherAssess tTeacherAssess = tTeacherAssessMapper.selectTTeacherAssessById(id);
        if (tTeacherAssess.getAuditState() == 3 ){
            throw new ServiceException("你已完成审核！！",2002);
        }
        tTeacherAssess.setAuditState(TTeacherAssess.FINISHED_EXAMINE);
        tTeacherAssessMapper.updateTTeacherAssess(tTeacherAssess);
        TCollectScore tCollectScore = new TCollectScore();
        tCollectScore.setTeacherName(tTeacherAssess.getTeacherName());
        tCollectScore.setScore(tTeacherAssess.getAuditScore());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        tCollectScore.setAuditName(tTeacherAssess.getAuditName());
        collectScoreService.insertTCollectScore(tCollectScore);
        return "审核通过";
    }

    /**
     * 生成本月绩效
     * @return
     */
    @Override
    public String generatePerformance() {
        //查询登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String username = loginUser.getUsername();
        //根据老师姓名查询老师所有信息
        AjaxResult ajaxResult = teacherService.findTeacherByName(username);
        if (!ajaxResult.isSuccess()){
            return "失败";
        }
        Object data = ajaxResult.get("data");
        TTeacher tTeacher = JSONObject.parseObject(JSONObject.toJSONString(data), TTeacher.class);
        //查询老师类别
        String tTeacherType = tTeacher.getType();
        List<String> projectsByTeacherType = performanceTemplateService.selectProjectBytTeacherType(tTeacherType);
        TTeacherAssess tTeacherAssess = new TTeacherAssess();
        projectsByTeacherType.forEach(project->{
            TPerformanceAssess performanceAssess = performanceAssessService.getProjectInfo(project);
            tTeacherAssess.setTeacherName(username);
            tTeacherAssess.setAssessMonth(new Date());
            tTeacherAssess.setProjectName(project);
            tTeacherAssess.setGoal(performanceAssess.getGoal());
            tTeacherAssess.setAuditName("admin");
            tTeacherAssess.setWeight(performanceAssess.getWeight());
            tTeacherAssess.setAuditState(TTeacherAssess.WAIT_ASSESS);
            tTeacherAssessMapper.insertTTeacherAssess(tTeacherAssess);
        });

        return "绩效生成成功";
    }
}
